﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OneKillProcess
{
    public partial class InputDialog : Form
    {
        private TextBox textBox1;
        private Button buttonOK;
        private Button buttonCancel;
        private Button buttonSearch;
        private ListBox listBoxProcesses;

        public string Keyword { get; private set; }

        public InputDialog()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            textBox1 = new TextBox();
            buttonOK = new Button();
            buttonCancel = new Button();
            buttonSearch = new Button();
            listBoxProcesses = new ListBox();

            this.Text = "输入关键字";

            textBox1.Location = new System.Drawing.Point(12, 12);
            textBox1.Size = new System.Drawing.Size(200, 20);

            buttonSearch.Location = new System.Drawing.Point(220, 12);
            buttonSearch.Text = "搜索";
            buttonSearch.Click += ButtonSearch_Click;

            listBoxProcesses.Location = new System.Drawing.Point(12, 40);
            listBoxProcesses.Size = new System.Drawing.Size(250, 150);
            listBoxProcesses.DoubleClick += ListBoxProcesses_DoubleClick;

            buttonOK.Location = new System.Drawing.Point(50, 200);
            buttonOK.Text = "确定";
            buttonOK.Click += ButtonOK_Click;

            buttonCancel.Location = new System.Drawing.Point(120, 200);
            buttonCancel.Text = "取消";
            buttonCancel.Click += ButtonCancel_Click;

            this.Controls.Add(textBox1);
            this.Controls.Add(buttonSearch);
            this.Controls.Add(listBoxProcesses);
            this.Controls.Add(buttonOK);
            this.Controls.Add(buttonCancel);

            this.Size = new System.Drawing.Size(400, 300);
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterParent;
        }

        private void ButtonSearch_Click(object sender, EventArgs e)
        {
            Keyword = textBox1.Text;
            listBoxProcesses.Items.Clear();
            SearchProcess();
        }

        private void SearchProcess()
        {
            foreach (var process in Process.GetProcesses())
            {
                if (process.ProcessName.ToLower().Contains(Keyword.ToLower()))
                {
                    MessageBox.Show(process.ProcessName);
                    listBoxProcesses.Items.Add(process.MainModule.FileName);
                }
            }
        }

        private void ListBoxProcesses_DoubleClick(object sender, EventArgs e)
        {
            if (listBoxProcesses.SelectedItem != null)
            {
                this.DialogResult = DialogResult.OK;
                Keyword = listBoxProcesses.SelectedItem.ToString();
                this.Close();
            }
        }

        private void ButtonOK_Click(object sender, EventArgs e)
        {
            if (listBoxProcesses.SelectedItem != null)
            {
                Keyword = listBoxProcesses.SelectedItem.ToString();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
